const express = require('express');
const proxy = require('http-proxy-middleware');
const path = require('path');
const app = express();
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const config = require('./config/index');

// 视图引擎设置
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 中间件
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'web')));
app.use(favicon(path.join(__dirname, 'public/favicon.ico')));

// 路由
app.use('/api', proxy({
  target: 'http://localhost:8080',
  changeOrigin: true,
  pathRewrite: {
    '^/api' : '/api'
  }
}));

app.listen(config.server.port, () => console.log('Server is listening at ' + config.server.host + ':' + config.server.port));
